package com.morn.fanfou;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.morn.fanfou.fragment.ConversationFragment;
import com.morn.fanfou.model.Message;
import com.morn.fanfou.model.User;

public class ConversationActivity extends FragmentActivity {

	private String otherId;

	private User user;

	private ConversationFragment conversationFragment;

	private TextView textView;

	private TextView countView;

	private AlertDialog clearDialog;

	private Intent serviceIntent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.conversation);

		otherId = this.getIntent().getStringExtra("otherId");
		if (otherId == null) {
			user = (User) getIntent().getSerializableExtra("user");
			otherId = user.getId();
		}
//		serviceIntent = new Intent(this, MessageService.class);
		serviceIntent.putExtra("otherId", otherId);
		this.startService(serviceIntent);
		initView();

	}

	public String getOtherId() {
		return otherId;
	}

	private void initView() {
		ImageView rightImage = (ImageView) findViewById(R.id.header_image_right);
		rightImage.setImageResource(R.drawable.ic_menu_upload);

		TextView titleText = (TextView) findViewById(R.id.header_title_text);
		titleText.setText((user != null) ? user.getName() : otherId);

		textView = (TextView) findViewById(R.id.conversation_text);
		countView = (TextView) findViewById(R.id.conversation_count);
		textView.addTextChangedListener(new MyTextWatcher(this, countView));

		FragmentManager fragmentManager = this.getSupportFragmentManager();
		conversationFragment = (ConversationFragment) fragmentManager
				.findFragmentById(R.id.conversation_fragment);

		clearDialog = new AlertDialog.Builder(this)
				.setTitle(R.string.notice)
				.setMessage(R.string.clear_text)
				.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								textView.setText("");
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
							}
						}).create();
	}

	public void clickLeftButton(View v) {

		this.finish();
	}

	public void clickRightButton(View v) {
		String str = textView.getText().toString();
		if (!TextUtils.isEmpty(str)) {
			Message message = new Message();
			message.setText(str);
			message.setRecipient_id(otherId);
			conversationFragment.addMessage(message);
			textView.setText("");
		} else {
			Toast.makeText(this, R.string.text_can_not_null, Toast.LENGTH_SHORT)
					.show();
		}
	}

	@Override
	protected void onDestroy() {
		this.stopService(serviceIntent);
		super.onDestroy();
	}

	public void clearTextClick(View v) {
		clearDialog.show();
	}

	public static class MyTextWatcher implements TextWatcher {

		private int strCount = 140;
		private TextView countView;

		private Toast toast;

		public MyTextWatcher(Context context, TextView countView) {
			toast = Toast.makeText(context, R.string.can_not_input,
					Toast.LENGTH_SHORT);
			this.countView = countView;
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			strCount = 140 - s.length();
			countView.setText(strCount + "");

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			if (strCount < 0) {
				s.delete(140, s.length());
				toast.show();
			}

		}

	}
}
